package com.zito.health.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zito.health.common.annotations.ResponseResult;
import com.zito.health.common.result.VerifyResult;
import com.zito.health.entity.Laboratory;
import com.zito.health.entity.LaboratoryExamine;
import com.zito.health.entity.LaboratoryExamineScore;
import com.zito.health.service.LaboratoryExamineScoreService;
import com.zito.health.service.LaboratoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: Sam
 * @time: 2020/4/11 14:26
 */
@Slf4j
@ResponseResult
@RestController
@RequestMapping("/laboratoryExamineScore")
public class LaboratoryExamineScoreController {

    @Autowired
    private LaboratoryExamineScoreService laboratoryExamineScoreService;
    @Autowired
    private LaboratoryService laboratoryService;

    /**
     *
     * @param
     * @return
     */
    @PostMapping()
    public VerifyResult examine(String laboratoryExamineScores){
        VerifyResult verifyResult = new VerifyResult();
        //
        JSONArray jsonArray = JSONArray.parseArray(laboratoryExamineScores);
        JSONObject jObject = (JSONObject)jsonArray.get(0);

        Laboratory laboratory = new Laboratory();
        laboratory.setId((Integer)jObject.get("laboratoryId"));
        laboratory = laboratoryService.queryOne(laboratory);

        //先删后增
        if(laboratory != null){
            laboratoryExamineScoreService.deleteBylaboratoryId(laboratory.getId());
        }
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject json = (JSONObject)jsonArray.get(i);
            LaboratoryExamineScore model= new LaboratoryExamineScore();
            model.setLaboratoryId((Integer)json.get("laboratoryId"));
            model.setSubjectId((Integer)json.get("subjectId"));
            System.out.println(json.get("whether"));
            model.setWhether(Integer.parseInt(json.get("whether").toString()));
            model.setRemarks((String)json.get("remarks"));
            laboratoryExamineScoreService.save(model);
        }

        return verifyResult;
    }

    @GetMapping("/getLabExamineScoreList")
    public List<LaboratoryExamineScore> getLaboratoryExamineScoreList(Integer laboratoryId){
        LaboratoryExamineScore param = new LaboratoryExamineScore();
        param.setLaboratoryId(laboratoryId);
        List<LaboratoryExamineScore> laboratoryExamines = laboratoryExamineScoreService.query(param);
        return laboratoryExamines;
    }

}
